/*
 * Copyright (C) 2016 Nishant Srivastava
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jk.dailydemo.screenShot;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jk.dailydemo.R;
import com.jk.dailydemo.RPResultListener;
import com.jk.dailydemo.RuntimePermissionUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import github.nisrulz.screenshott.ScreenShott;

public class ScreenShot extends AppCompatActivity {
    @BindView(R.id.imageView)
    ImageView imageView;
    private Bitmap bitmap;
    @BindView(R.id.hidden_txtview)
    TextView hidden_txtview;

    private final static String[] requestWritePermission =
            {Manifest.permission.WRITE_EXTERNAL_STORAGE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_shot);
        ButterKnife.bind(this);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("ScreenShot");
//        setSupportActionBar(toolbar);
    }

    @OnClick({R.id.capture_screenshot, R.id.capture_refresh, R.id.capture_save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.capture_screenshot:
//                bitmap = ScreenShott.getInstance().takeScreenShotOfView(imageView);
//        bitmap = ScreenShott.getInstance().takeScreenShotOfJustView(hidden_txtview);
                bitmap = ScreenShott.getInstance().takeScreenShotOfRootView(view);

                imageView.setImageBitmap(bitmap);
                break;
            case R.id.capture_refresh:
                Bitmap placeholder =
                        BitmapFactory.decodeResource(ScreenShot.this.getResources(), R.drawable.placeholder);

                imageView.setImageBitmap(placeholder);

                break;
            case R.id.capture_save:

                final boolean hasWritePermission = RuntimePermissionUtil.checkPermissonGranted(this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE);
                if (bitmap != null) {
                    if (hasWritePermission) {
                        saveScreenshot();
                    } else {
                        RuntimePermissionUtil.requestPermission(ScreenShot.this, requestWritePermission, 100);
                    }
                }
                break;


        }

    }


    private void saveScreenshot() {

        ScreenShott.getInstance()
                .saveScreenshotToPicturesFolder(ScreenShot.this, bitmap, "my_screenshot");

        Toast.makeText(ScreenShot.this, "Bitmap Saved!", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull final String[] permissions,
                                           @NonNull final int[] grantResults) {
        switch (requestCode) {
            case 100: {

                RuntimePermissionUtil.onRequestPermissionsResult(grantResults, new RPResultListener() {
                    @Override
                    public void onPermissionGranted() {
                        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                            saveScreenshot();
                        }
                    }

                    @Override
                    public void onPermissionDenied() {
                        Toast.makeText(ScreenShot.this, "Permission Denied! You cannot save image!", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            }
        }
    }
}
